package cn.xinfei.xdecision.engine.runner.api;

import cn.xinfei.xdecision.common.datamodel.DecisionResult;
import cn.xinfei.xdecision.common.model.enginex.model.RuleHitModel;
import cn.xinfei.xdecision.common.model.enginex.model.RuleSetDecisionResultData;
import cn.xinfei.xdecision.engine.runner.metadata.PrometheusTagsConstants;
import cn.xinfei.xdecision.engine.runner.service.DecisionResultDetailService;
import cn.xinfei.xdecision.engine.runner.service.DecisionResultService;
import cn.xinfei.xdecision.model.DecisionApiResponse;
import cn.xinfei.xdecision.model.DecisionDataApiRequest;
import io.micrometer.core.instrument.Metrics;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class DecisionDataApi {

    @Autowired
    private DecisionResultService decisionResultService;

    @Autowired
    private DecisionResultDetailService decisionResultDetailService;


    public DecisionApiResponse query(DecisionDataApiRequest decisionDataApiRequest) {
        String businessId = decisionDataApiRequest.getRequestId();
        String decisionId = decisionDataApiRequest.getDecisionId();
        DecisionApiResponse response = new DecisionApiResponse();
        long start = System.currentTimeMillis();
        List<DecisionResult> decisionResultList = decisionResultService.getDecisionResultByBizIdAndDecisionId(businessId, decisionId);
        long end = System.currentTimeMillis();
        log.info("queryDuration-step1:{}", end - start);
        if (null != decisionResultList && decisionResultList.size() > 0) {
            DecisionResult decisionResult = decisionResultList.get(0);
            BeanUtils.copyProperties(decisionResult, response);
            response.setVersionNo(decisionResult.getEngineVersion());
            String decId = decisionResult.getDecisionId();
            String engineCode = decisionResult.getEngineCode();
            boolean hasContext = decisionDataApiRequest.isHasContext();
            if (hasContext) {
                Document snapshot = decisionResultDetailService.getDecisionResultDetailByDecisionId(decId, engineCode);
                long end2 = System.currentTimeMillis();
                log.info("queryDuration-step2:{}", end2 - end);
                if (null != snapshot) {
                    Map<String, Object> output = snapshot.get("output", Map.class);
                    Map<String, Object> in = snapshot.get("input", Map.class);
                    List<RuleHitModel> hitResultList = snapshot.get("hitResultList", List.class);
                    List<RuleSetDecisionResultData> branchRejectInfo = snapshot.get("branchRejectInfo", List.class);
                    response.setOut(output);
                    response.setIn(in);
                    response.setHitResultList(hitResultList);
                    response.setBranchRejectInfo(branchRejectInfo);
                }
            }
        }
        long end3 = System.currentTimeMillis();
        log.info("queryDuration-step3:{}", end3 - start);
        Metrics.gauge("xengine_query_respond", PrometheusTagsConstants.XENGINE_API_RESPOND_TAGS, end3 - start);
        return response;
    }
/*
    public List<DecisionDataApiResponse> queryList(DecisionDataApiRequest decisionDataApiRequest) {
        List<DecisionDataApiResponse> result = new ArrayList<>();
        String businessId = decisionDataApiRequest.getRequestId();
        String decisionId = decisionDataApiRequest.getDecisionId();
        List<DecisionResult> decisionResultList = decisionResultService.getDecisionResultByBizIdAndDecisionId(businessId, decisionId);
        if (null != decisionResultList) {
            for (int i = 0; i < decisionResultList.size(); i++) {
                DecisionResult decisionResult = decisionResultList.get(i);
                String decId = decisionResult.getDecisionId();
                String engineCode = decisionResult.getEngineCode();
                DecisionDataApiResponse response = new DecisionDataApiResponse();
                BeanUtils.copyProperties(decisionResult, response);
                boolean hasContext = decisionDataApiRequest.isHasContext();
                if (hasContext) {
                    List<Document> decisionSnapshot = decisionResultDetailService.getDecisionResultDetailByDecisionId(decId, engineCode);
                    if (null != decisionSnapshot) {
                        for (int j = 0; j < decisionSnapshot.size(); j++) {
                            Document snapshot = decisionSnapshot.get(j);
                            String branch = snapshot.getString("branch");
                            Map<String, Object> output = snapshot.get("output", Map.class);
                            Map<String, Object> in = snapshot.get("input", Map.class);
                            Map<String, Object> context = snapshot.get("context", Map.class);
                            response.setOut(output);
                            response.setIn(in);
                            response.setContext(context);
                            response.setBranch(branch);
                        }
                    }
                }
                result.add(response);
            }
        }
        return result;

    }*/

}
